//
//  ViewController.swift
//  Swift2048
//
//  Created by Miss.Ou on 16/8/9.
//  Copyright © 2016年 Miss.Ou. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBAction func startGame(sender: AnyObject) {
        
        let alertController = UIAlertController(title: "开始 !",message: "游戏就要开始,你准备好了吗?", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Ready Go!", style: UIAlertActionStyle.Default, handler: {
        
            action in self.presentViewController(MainTableViewController(), animated: true, completion: nil)
        
        }))
        self.presentViewController(alertController, animated: true, completion: nil)
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

class MainViewController: UIViewController {
    
    //游戏方格维度
    var dimension:Int = 4
    //游戏过关最大值
    var maxnumber:Int = 2048
    
    //数字格子的宽度
    var width:CGFloat = 50
    //格子与格子之间的距离
    var padding:CGFloat = 6
    
    //保存背景图数据
    var backgrounds:Array<UIView>!
    
  
    
    
    init(){
        self.backgrounds = Array<UIView>()
        super.init(nibName:nil, bundle:nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.whiteColor()
        
    
        self.setupGamemap()
        self.setupScoreLabels()
        
    }
    
    func setupGamemap(){
        var x:CGFloat = 50
        var y:CGFloat = 150
        
        for i in 0..<dimension {
            print(i)
            
            y = 150
            
            for j in 0..<dimension {
                
                
                //初始化视图
                let background = UIView(frame:CGRectMake(x, y, width, width))
                background.backgroundColor = UIColor.darkGrayColor()
                
                self.view.addSubview(background)
                
                //将视图保存起来,以备后用
                backgrounds.append(background)
                
                y += padding + width
                
            }
            
            x += padding + width
        }
        
    }
    
    func setupScoreLabels(){
        let score = ScoreView(stype: ScoreType.Common)
        score.frame.origin = CGPointMake(50, 80)
        score.changeScore(value: 0)
        self.view.addSubview(score)
        
        let bestscore = ScoreView(stype: ScoreType.Best)
        bestscore.frame.origin.x = 170
        bestscore.frame.origin.y = 80
        bestscore.changeScore(value: 0)
        self.view.addSubview(bestscore)
        
    }
    
    
}
class MainSettingViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.whiteColor()
    }
    
}

class MainTableViewController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //一共包含两个视图
        let viewMain = MainViewController()
        viewMain.title = "2048"
        
        let viewSetting = MainSettingViewController()
        viewSetting.title = "设置"
        
        //分别声明两个控制器
        let main = UINavigationController(rootViewController: viewMain)
        let setting = UINavigationController(rootViewController: viewSetting)
        
        self.viewControllers = [
            main, setting
        ]
        
        //设置默认选中的是游戏的主界面视图
        self.selectedIndex = 0
        
    }
}

enum ScoreType {
    case Common //普通分数
    case Best //最高分数
}

protocol ScoreViewProtocol {
    func changeScore(value s:Int)
}

class ScoreView: UIView, ScoreViewProtocol
{
    
    var label:UILabel!
    
    let defaultFrame = CGRectMake(0, 0, 100, 30)
    var stype:String! //显示分数是最低分还是最高分
    var score:Int = 0{
    
        didSet{
        
            //分数变化,标签内容也变换
            label.text = "\(stype):\(score)"
        }
    }
    
    //传入分数面板的类型,用于控制标签的显示
    init(stype:ScoreType){
        label = UILabel(frame: defaultFrame)
        label.textAlignment = NSTextAlignment.Center
        
        super.init(frame: defaultFrame)
        
        self.stype = (stype == ScoreType.Common ? "分数" : "最高分")
        
        backgroundColor = UIColor.orangeColor()

        label.font = UIFont(name: "微软雅黑", size: 16)
        label.textColor = UIColor.whiteColor()
        self.addSubview(label)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    func changeScore(value s: Int) {
        score = s
    }
    
   
}


